---
title: "Untitled"
output: html_document
date: "2023-08-22"
---
############################################
Title: 3_02_Figure_2.rmd
Purpose: This code creates Figure 2.
Last updated: 25-04-2023

Input:
    - Baseline aggregate macro data at the monthly frequency. Latest version: final_agg_macro_data_monthly.csv 

Output:
    - Figure 2

##########################################


0. Setup *Set your file path here*
```{r, results="hide", include=FALSE}
#Clear user defined environment
rm(list=ls())
gc()

#Load packages
library(pacman)
p_load(readxl, lubridate, dplyr, tidyverse, eFRED)

#options
options(digits=4)
options(stringsAsFactors = FALSE)
options(scipen=999)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise

#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"

```

1. Figure 2
```{r, results="hide", include=FALSE}


#Load
final_agg_macro <- read_csv(paste0(global, "/Data/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))


#Add Recession bars
recessions.df = read.table(textConnection( "Peak, Trough
1981-07-01, 1982-11-01
1990-07-01, 1991-03-01
2001-03-01, 2001-11-01
2007-12-01, 2009-06-01
2020-03-01, 2020-12-01
2022-03-01, 2022-04-01"), sep=',',
colClasses=c('Date', 'Date'), header=TRUE)
recessions.trim = subset(recessions.df, Peak >= min(final_agg_macro$date) )

#Plot
fig = final_agg_macro %>%
  ggplot() +
  geom_line( aes(x=date,y=agg_loan_spr), colour= 'blue') +
  geom_line( aes(x=date,y=baa_aaa), colour= 'black') +
  geom_line( aes(x=date,y=cp_treas3mo), colour= 'purple') +
  geom_line( aes(x=date,y=gz_spread), colour= 'red') +
  ylab(" ") +
  xlab("") +
  geom_hline(yintercept=0) +
  theme_bw() + 
  theme(strip.text.x = element_text(size=10),
          strip.text.y = element_text(size=10),
          strip.background = element_rect(colour="white", fill="white")) +
  theme(axis.text=element_text(size=10)) + 
  theme(axis.title.x = element_text(size = 10)) +
  theme(axis.title.y = element_text(size = 10)) +
  theme(plot.title = element_text(color="black", size=10, face = "bold"))


fig = fig + geom_rect(data=recessions.trim, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='Blue', alpha=0.2)




```

